<?php

namespace App\Jobs\Train\TrainOrder;

use App\Models\Orders\Repositories\Interfaces\OrdersInterface;
use App\Models\Trip\OrderPay\OrderPay;
use App\Models\Trip\TripOrderCosts\Repositories\Interfaces\TripOrderCostsRepositoryInterface;
use App\Models\Trip\TripOrderCosts\Repositories\Interfaces\TripOrderPassengerCostsRepositoryInterface;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class ProcessUpdateTripCostPaySn implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private OrdersInterface $order_info;
    private OrderPay        $pay_info;
    private int             $scope;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(OrdersInterface $order, OrderPay $pay_info, int $scope = 0)
    {
        $this->order_info = $order;
        $this->pay_info   = $pay_info;
        $this->scope      = $scope;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        // 作用域 全局
        $costRepo          = app(TripOrderCostsRepositoryInterface::class);
        $passengerCostRepo = app(TripOrderPassengerCostsRepositoryInterface::class);

        if ($this->scope == 0
            && in_array($this->pay_info->getBusinessType(), [
                OrderPay::BUSINESS_TYPE_ORDER,
                OrderPay::BUSINESS_TYPE_CHANGE,
                OrderPay::BUSINESS_TYPE_SNAP,
            ])
        ) {
            $costRepo->updatePaySnByOrder($this->order_info, $this->pay_info->getPaySn());
            $passengerCostRepo->updatePaySnByOrder($this->order_info, $this->pay_info->getPaySn());
        }


    }
}
